<template>
  <div>
    <el-form class="query-form" :inline="true" :model="params">
      <el-form-item>
        <el-input v-model="params.load_name" placeholder="产品名称查询"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item class="float-right">
        <el-button type="primary" @click="handleEditProduct()" icon="el-icon-plus" v-show="power">
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
      :row-class-name="goodsRowClass"
    >
      <template v-slot:loadName="scope">
        <div>{{ scope.row.load_name }}</div>
        <small v-if="+scope.row.shelves_status === 2" class="text-warning">（产品已下架）</small>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <product-edit-dialog
      :visible.sync="editFormVisible"
      :form-data="editFormData"
      :table-id="editTableId"
      @success="getData()"
    ></product-edit-dialog>

    <goods-view-dialog :visible.sync="viewFormVisible" :goods-id="viewFormId"></goods-view-dialog>
  </div>
</template>
,

<script>
import { mapGetters } from 'vuex'
import ProductEditDialog from '@/bank/views/product/ProductEditDialog'
import GoodsViewDialog from '@/bank/views/product/GoodsViewDialog'
export default {
  components: {
    ProductEditDialog,
    GoodsViewDialog,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
      },
      order: { create_time: 'desc' },
      loading: false,
      tableName: 'industrial_package',
      totalCount: 0,
      tableData: [],
      tableColumns: [
        { attrs: { label: '产品名', prop: 'load_name', minWidth: 120 }, slot: 'loadName' },
        { attrs: { label: '支持产业', prop: 'supply_product_name', minWidth: 100 } },
        { attrs: { label: '适用区域', prop: 'supply_address_code_name', minWidth: 120 } },
        { attrs: { label: '适用对象', prop: 'load_entity', minWidth: 130 } },
        { attrs: { label: '担保额度(万元)', prop: 'fundsRang' } },
        { attrs: { label: '融资期限(月)', prop: 'yearRang' } },
        { attrs: { label: '贷款利率', prop: 'bank_rate' } },
        {
          attrs: { label: '操作', align: 'center', width: 150, fixed: 'right' },
          operations: [
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.handleView,
            },
            {
              content: '编辑产品信息',
              icon: 'fa fa-edit',
              event: this.handleEditProduct,
              ifRender: row => this.power && row.service_id === this.clientId,
            },
            {
              content: row => (+row.shelves_status === 1 ? '下架' : '上架'),
              icon: 'fa fa-exchange transform-90deg',
              event: this.handleShelf,
              ifRender: row => this.power && row.service_id === this.clientId,
            },
            {
              content: '删除',
              icon: 'fa fa-trash',
              event: this.handleDelete,
              attrs: { class: 'text-danger py-0 px-2' },
              ifRender: row => this.power && row.service_id === this.clientId,
            },
          ],
        },
      ],
      editFormData: {},
      editTableId: '',
      editFormVisible: false,

      viewFormId: '',
      viewFormVisible: false,
    }
  },
  computed: {
    ...mapGetters(['job', 'clientId']),
    power() {
      return this.job === 'org_corp'
    },
  },
  watch: {},
  created() {
    this.params.supply_agency_id = this.clientId
    this.getData()
  },
  mounted() {},
  methods: {
    handleView(row = {}) {
      this.viewFormId = row?.industrial_package_id ?? ''
      this.viewFormVisible = true
    },
    async handleDelete(row) {
      this.$utils
        .msgbox({
          message: `确定删除 ${row.load_name || ''}? 若有项目使用过请谨慎删除！`,
          type: 'warning',
          beforeClose: () =>
            this.$api.deleteData(this.$serviceName, this.tableName, row.industrial_package_id),
        })
        .then(() => {
          this.$message.success('删除成功！')
          this.getData()
        })
        .catch(() => this.$message.error('删除失败，请重试！'))
    },
    handleEditProduct(row = {}) {
      this.editFormData = { ...row }
      this.editTableId = row?.industrial_package_id ?? ''
      this.editFormVisible = true
    },
    handleShelf(row) {
      const isOnShelf = +row.shelves_status === 1
      const shelfLabel = isOnShelf ? '下架' : '上架'
      this.$utils
        .msgbox({
          message: `确定${shelfLabel}产品 ${row.load_name || ''}?`,
          type: 'warning',
          beforeClose: () =>
            this.$api.updateData(this.$serviceName, this.tableName, row.industrial_package_id, {
              shelves_status: isOnShelf ? 2 : 1,
            }),
        })
        .then(() => {
          this.$message.success(`${shelfLabel}成功！`)
          this.getData()
        })
        .catch(() => this.$message.error(`${shelfLabel}失败，请重试！`))
    },
    goodsRowClass({ row }) {
      const isOnShelf = +row.shelves_status === 1
      return isOnShelf ? '' : 'off-shelf'
    },
    getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = (data.data || []).map(this.dataAdapter)
          this.totalCount = data.totalCount || 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      item.fundsRang = `${item.load_amount_min || ''}~${item.load_amount || ''}`
      item.yearRang = `${item.loan_term_min || ''}~${item.loan_term || ''}`
      return item
    },
  },
}
</script>

<style lang="scss" scoped>
/deep/ .off-shelf {
  background-color: #fdf6ec;
  border-color: #faecd8;
  color: #e6a23c;
}
/deep/ .transform-90deg {
  transform: rotate(90deg);
}
</style>
